package org.jeecg.modules.deepseek.tools;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.deepseek.SqlQueryChecker;
import org.jeecg.modules.demo.wuyou.service.ISitemapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Configuration
public class WebSchoolmxTools {
    @Autowired
    ISitemapService sitemapService;
    @Schema(description = "查询学校所需信息所需字段，包括查询条件和排序条件。把查询SQL放在SelectSql字段中，只查询主要字段，不需要查询所有字段")
    public record QueryWebSchoolmx(
        @Schema(description = "只查询主要字段，不需要查询所有字段" +
                "create table web_bar\n" +
                "(\n" +
                "    id     varchar(100) not null\n" +
                "        primary key,\n" +
                "    NAME   varchar(50)  null comment '名称',\n" +
                "    BARURL varchar(255) null comment '请求地址',\n" +
                "    row_id varchar(255) null comment '系统id',\n" +
                "    SEQ_ID int          not null comment '排序名称'\n" +
                ")\n" +
                "    comment '导航栏' collate = utf8mb4_unicode_ci;")
        String selectSql,
        @Schema(description = "学校表结构详情：create table web_hotshoolmx\n" +
                "(\n" +
                "    id              varchar(100)             not null\n" +
                "        primary key,\n" +
                "    hotshool_id     varchar(100)             not null comment 'hotshoolId',\n" +
                "    row_id          varchar(255)             null comment 'row_id',\n" +
                "    SCHOOLNAME      varchar(50)   default '' null comment '学校名称',\n" +
                "    ENGTHSCHOOLNAME varchar(100)  default '' null comment '英文名称',\n" +
                "    IMAGEURL        varchar(255)  default '' null comment '图片地址',\n" +
                "    SCHOOLTOP       varchar(100)  default '' null comment '学校排名',\n" +
                "    SCHOOLTYPE      varchar(100)  default '' null comment '学校类型',\n" +
                "    SCHOOLPER       varchar(100)  default '' null comment '国际生比',\n" +
                "    SCHOOLIN        varchar(100)  default '' null comment '录取率',\n" +
                "    SCHOOLOUT       varchar(100)  default '' null comment '毕业率',\n" +
                "    SCHOOLPRICE     varchar(100)  default '' null comment '学费',\n" +
                "    SEQ_ID          int                      null comment '排序名称',\n" +
                "    explain_text    text                     null comment '学校简介',\n" +
                "    APPLAYNUMBER    varchar(255)             null comment '申请人数',\n" +
                "    SCHOOLADDRESS   varchar(255)  default '' null comment '学校地址',\n" +
                "    SCHOOLGUANGWA   varchar(255)             null comment '学校官网',\n" +
                "    SHIJEUSNEWS     int                      null comment '世界USNEWS排名',\n" +
                "    SHIJIEQS        int                      null comment '世界(QS)排名',\n" +
                "    JIUYELV         varchar(50)   default '' null comment '就业率',\n" +
                "    BIYXINZI        varchar(50)   default '' null comment '毕业薪资',\n" +
                "    XUESZONGSU      varchar(50)   default '' null comment '学生总数量',\n" +
                "    JIANGJIUSZONGS  varchar(50)   default '' null comment '研究生数量',\n" +
                "    BENKEZONGS      varchar(50)   default '' null comment '本科生数量',\n" +
                "    GUOJIXUESBL     varchar(50)   default '' null comment '国际学生比例',\n" +
                "    NANNVBL         varchar(50)   default '' null comment '男女比例',\n" +
                "    QIANGSHIZY      varchar(1000) default '' null comment '强势专业',\n" +
                "    REMENGZY        varchar(255)  default '' null comment '热门专业',\n" +
                "    XUANZEYXLY      text                     null comment '选择该校的理由',\n" +
                "    DILIWEIZHI      text                     null comment '地理位置',\n" +
                "    XUEXIAOLS       text                     null comment '学校历史',\n" +
                "    XUEXIAOHJ       text                     null comment '校园环境',\n" +
                "    XUEXIAOTS       text                     null comment '学校特色',\n" +
                "    YUANXISHEZHI    text                     null comment '院系设置',\n" +
                "    JIAOYETES       text                     null comment '教学特色',\n" +
                "    ZSBXX           text                     null comment '招生办信息',\n" +
                "    id_order_shool  int(10)                  null comment 'idOrderShool'\n" +
                ")\n" +
                "    comment '热门院校详情' collate = utf8mb4_unicode_ci;" +
                "国家表结构详情：create table web_hotshool\n" +
                "(\n" +
                "    id           varchar(100) not null\n" +
                "        primary key,\n" +
                "    NAME         varchar(50)  null comment '国家名称',\n" +
                "    row_id       varchar(255) null comment 'row_id',\n" +
                "    SEQ_ID       int          not null comment '排序名称',\n" +
                "    BZ           varchar(255) null comment '备注',\n" +
                "    PUSHURL      varchar(255) null comment '跳转地址',\n" +
                "    ENGLISH_NAME varchar(64)  null comment '英文地址'\n" +
                ")\n" +
                "    comment '国家表信息' collate = utf8mb4_unicode_ci;")
        String tableStructure,
        @Schema(description = "国家表示例数据{\n" +
                "    \"id\": \"8565d946-1a49-4e0b-b109-6691e88dcc2f\",\n" +
                "    \"hotshool_id\": \"89beee9f-9adb-4aab-9cea-79eaf5b35e6a\",\n" +
                "    \"row_id\": null,\n" +
                "    \"SCHOOLNAME\": \"麻省理工学院\",\n" +
                "    \"ENGTHSCHOOLNAME\": \"Massachusetts Institute of Technology\",\n" +
                "    \"IMAGEURL\": \"https://cdn.api2.51liuxue.com/school_image/logo/5mAhqqO8uvXcvZm5S5BI1ld8qOo4.jpg\",\n" +
                "    \"SCHOOLTOP\": \"2\",\n" +
                "    \"SCHOOLTYPE\": \"私立\",\n" +
                "    \"SCHOOLPER\": \"1:11.1\",\n" +
                "    \"SCHOOLIN\": \"16%\",\n" +
                "    \"SCHOOLOUT\": \"98.1%\",\n" +
                "    \"SCHOOLPRICE\": \"$39,560~$62,250\",\n" +
                "    \"SEQ_ID\": 2,\n" +
                "    \"explain_text\": \"麻省理工学院（Massachusetts Institute of Technology，缩写MIT）成立于1861年，位于美国马萨诸塞州波士顿附近的剑桥市，其吉祥物为海狸，代表动物界最擅长筑水坝的工程师。\",\n" +
                "    \"APPLAYNUMBER\": \"未公布\",\n" +
                "    \"SCHOOLADDRESS\": \"美国-马萨诸塞-波士顿\",\n" +
                "    \"SCHOOLGUANGWA\": \" \",\n" +
                "    \"SHIJEUSNEWS\": 2,\n" +
                "    \"SHIJIEQS\": 2,\n" +
                "    \"JIUYELV\": \"本科91.6%；研究生93.8%\",\n" +
                "    \"BIYXINZI\": \"$85,000\",\n" +
                "    \"XUESZONGSU\": \"72,207\",\n" +
                "    \"JIANGJIUSZONGS\": \"不详\",\n" +
                "    \"BENKEZONGS\": \"\",\n" +
                "    \"GUOJIXUESBL\": \"\",\n" +
                "    \"NANNVBL\": \"\",\n" +
                "    \"QIANGSHIZY\": \"\",\n" +
                "    \"REMENGZY\": \"\",\n" +
                "    \"XUANZEYXLY\": null,\n" +
                "    \"DILIWEIZHI\": null,\n" +
                "    \"XUEXIAOLS\": null,\n" +
                "    \"XUEXIAOHJ\": null,\n" +
                "    \"XUEXIAOTS\": null,\n" +
                "    \"YUANXISHEZHI\": null,\n" +
                "    \"JIAOYETES\": null,\n" +
                "    \"ZSBXX\": null,\n" +
                "    \"id_order_shool\": 1\n" +
                "  }" +
                "国家表示例数据：{\n" +
                "    \"id\": \"89beee9f-9adb-4aab-9cea-79eaf5b35e6a\",\n" +
                "    \"NAME\": \"美国\",\n" +
                "    \"row_id\": \"13ae0727-9fa7-48f2-8855-089f3767a8ec\",\n" +
                "    \"SEQ_ID\": 1,\n" +
                "    \"BZ\": \"高中 预科 本科 硕士 博士\",\n" +
                "    \"PUSHURL\": \"webwyjbinfogwtd\",\n" +
                "    \"ENGLISH_NAME\": \"America\"\n" +
                "  }")
        String sampleData
    ) {
    }

    @Bean
    @Description("根据条件生成SQL，查询学校信息")
    public Function<QueryWebSchoolmx, List<Map<String, Object>>> getWebSchoolMxBySelected() {
        return request -> {
            try {
                String s = SqlQueryChecker.validateAndModifySql(request.selectSql, new String[]{"web_hotshool", "web_hotshoolmx"});
                log.debug("查询条件{}", s);
                List<Map<String, Object>> maps1 = sitemapService.GetByCondition(s, new HashMap<>());

//                List<Map<String, Object>> maps = sitemapService.GetByCondition("select db_field_name,db_field_txt\n" +
//                        "from onl_cgform_field left join onl_cgform_head oci on oci.id = onl_cgform_field.cgform_head_id\n" +
//                        "where oci.table_name in('web_hotshool','web_hotshoolmx','web_teammx');", new HashMap<>());
//
//                Map<String, Object> resultMap = maps.stream()
//                        .collect(Collectors.toMap(
//                                map -> (String) map.get("db_field_name"),
//                                map -> (Object) map.get("db_field_txt"),
//                                (existingValue, newValue) -> existingValue
//                        ));
//                maps1.add(resultMap);
                return maps1;
            } catch (Exception e) {
                log.warn("查询失败{}", e.getMessage());
                return null;
            }
        };
    }
}
